<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%
String path = request.getContextPath();
 %>
<html>
	<head>
		<base target="_self" />
		<meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
		<title>新增客户信息</title>
		<link rel="stylesheet" type="text/css" href="<c:url value="/static/jquery-easyui/themes/default/easyui.css"/>">
		<link rel="stylesheet" type="text/css"
			href="<c:url value="/static/jquery-easyui/themes/icon.css"/>">
		<script type="text/javascript"
			src="<c:url value="/static/jquery-easyui/jquery-1.6.min.js"/>"></script>
		<script type="text/javascript"
			src="<c:url value="/static/jquery-easyui/jquery.easyui.min.js"/>"></script>
		<script type="text/javascript"
			src="<c:url value="/static/jquery-easyui/plugins/jquery.panel.js"/>"></script>
		<script type="text/javascript"
			src="<c:url value="/static/jquery-easyui/plugins/jquery.datagrid.js"/>"></script>
		<script type="text/javascript"
			src="<c:url value="/static/jquery-easyui/plugins/jquery.messager.js"/>"></script>
		<script type="text/javascript"
			src="<c:url value="/jsp/base/queryCode.js"/>"></script>
		<script type="text/javascript"
			src="<c:url value="/static/jquery-easyui/ext-for-framework.js.jsp"/>"></script>
		<script type="text/javascript" src="<c:url value="/static/jquery-easyui/locale/easyui-lang-zh_CN.js"/>"></script>		

 
    <script type="text/javascript">
          var certObj=$.ajax({url:"<%=path %>/querycode/listcode.do?codeTypeId=certificate_type",async:false});
          var certComboData = eval(certObj.responseText);
          var contTypeObj=$.ajax({url:"<%=path %>/querycode/listcode.do?codeTypeId=contact_type",async:false});
          var contTypeComboData = eval(contTypeObj.responseText);
          var addTypeObj=$.ajax({url:"<%=path %>/querycode/listcode.do?codeTypeId=address_type",async:false});
          var addTypeComboData = eval(addTypeObj.responseText);
          
         //信息初始化
         function initList(listDisplayEId) {
				$('#' + listDisplayEId).datagrid(
						{
							nowrap : false,
							striped : true,
							collapsible : true,
							remoteSort : true,
							notSelectByRow : false,
							frozenColumns : [ [ {
								field : 'ids',
								checkbox : true
							} ] ],
							toolbar : [
									{
										id : listDisplayEId + '_btn_add',
										text : '新增',
										iconCls : 'icon-add',
										handler : function() {
  											$('#' + listDisplayEId).datagrid('endEdit', lastIndex);
											$('#' + listDisplayEId).datagrid('appendRow',{
 												 
											});
											var lastIndex = $('#' + listDisplayEId).datagrid('getRows').length-1;
											$('#' + listDisplayEId).datagrid('selectRow', lastIndex);
											$('#' + listDisplayEId).datagrid('beginEdit', lastIndex);
										}
							},'-' ,{
						id : listDisplayEId + '_btn_del',
						text : '删除',
						iconCls : 'icon-cancel',
						handler : function() {
					var row = $('#'+listDisplayEId).datagrid('getSelections');		
					if(row.length == 0){
						$.messager.alert('提示','请选择一条记录操作！','info');
						return;
					}else{
 					    $('#'+listDisplayEId).datagrid('deleteRow', $('#'+listDisplayEId).datagrid('getRowIndex',row[0]));
   					}
  				  }
				}]  ,
							onLoadError:function(){
								$.messager.alert('错误','获取数据失败，请与管理员联系！','error');
							}
						});
			};
       
 			  //信息初始化
	        function initCertList(listDisplayEId) {
				$('#' + listDisplayEId).datagrid(
						{
								nowrap : false,
								striped : true,
								collapsible : true,
								remoteSort : true,
								notSelectByRow : false,
								frozenColumns : [ [ {
									field : 'ids',
									checkbox : false
								} ] ],
								onLoadError:function(){
									$.messager.alert('错误','获取数据失败，请与管理员联系！','error');
								}
							});
  				for(var i = 0; i < certComboData.length; i++){
 					$('#' + listDisplayEId).datagrid('appendRow',{
						certTypeId: certComboData[i].codeId,
						certTypeName: certComboData[i].codeName
					});
 					$('#' + listDisplayEId).datagrid('beginEdit', i);
				}
			 };
	       
				
       	//添加信息按钮响应函数
		function addCustomer(){
			 var birthday = $("#birthday").datebox("getValue");
			 if(!isDate(birthday)){
			      alert("出生日期格式有误\n\r日期格式：yyyy-mm-dd");
			      return;
			 }
       		 var rows = getRows($('#certificateList'));
       		 if(rows === false){
       			 return;
       		 }
       		 $("#certRows").val(rows);
  		     rows = getRows($('#contactList'));
		     if(rows === false){
       			 return;
       		 }
       		 $("#contRows").val(rows);
		     rows = getRows($('#addressList'));
		     if(rows === false){
       			 return;
       		 }
       		 $("#addrRows").val(rows);
  		     $('#AddCustomerForm').form('submit',
			{
				url:encodeURI("<c:url value='/customer/saveCustomer.do'/>"), //查询提交路径
 				onSubmit:function()//提交按钮相应函数
				{ 
				     return $('#AddCustomerForm').form('validate');
				},
				success:function(data){
 				    data = eval('(' + data + ')');
 	       			if(data.success){
	       				$.messager.alert("提示", "新增成功！", 'info',function(){
		       				window.close();
	       				});
       				}else{
	       				$.messager.alert("提示", data.msg, 'warning');
       				}
				},
				error:function(){
					$.messager.alert("错误", "系统错误，请联系管理员！", 'error');
					return;
				}
			});
 			
 		 
		}
		
		
		//添加信息按钮响应函数
         function getRows(dgObj){
             var rows = dgObj.datagrid('getRows');
   			 if(rows.length != 0){
				for(var i = 0; i < rows.length; i++){
				    if(!dgObj.datagrid('validateRow', i)){
				        $.messager.alert("提示",  "表格数据校验没通过！", 'warning');
				        return false;
				    }
 				    dgObj.datagrid('endEdit', i);  
   				} 
 			 } 
   		     var strArray = new Array();
   			 for(var i = 0; i < rows.length; i++){
   			     var rowStr = '{';
   				 var row = rows[i];
   				 for(var prop in row){
   					rowStr = rowStr + '"'+prop+'":';
   					rowStr = rowStr + '"'+(row[prop]==null ? '': row[prop])+'",';
   				 }
   				var s = rowStr.substr(0, rowStr.length - 1) + "}";
    		    strArray.push(s);
   			 }
   			 return "[" + strArray.join() + "]";
            }
		
	
       
			$(document).ready(function() {
				initCertList('certificateList');
				initList('contactList');
				initList('addressList');
 				formatCombobox($("#gender"),"<%=path %>/querycode/listcode.do?codeTypeId=gender");
 				formatCombobox($("#degreeId"),"<%=path %>/querycode/listcode.do?codeTypeId=degree");
   				formatCombobox($("#nationId"),"<%=path %>/querycode/listcode.do?codeTypeId=nation");
   				formatCombobox($("#isSmoking"),"<%=path %>/querycode/listcode.do?codeTypeId=is_smoking");
   				formatCombobox($("#country"),"<%=path %>/querycode/listcode.do?codeTypeId=country");
   				formatCombobox($("#marriageId"),"<%=path %>/querycode/listcode.do?codeTypeId=marriage");
   				 
   				$("#birthday").combo({
   					onChange:function(newValue, oldValue){
    					 if(!isMaybeDate(newValue)){
    						 alert("日期格式有误，格式为yyyy-MM-dd,例如2014-10-22");
    					 }
   					}
   				});
   			});
 			 
			 
    </script>
   
  </head>

  <body>
	  <form id="AddCustomerForm" name="AddCustomerForm" method="post">
		
		  <div style="height: auto; background: #fafafa; padding: 5px;" class="easyui-panel" title="基本信息" iconCls="icon-save" 
		  collapsible="true" minimizable="false" maximizable="false" closable="false">
			
			   <table cellpadding="5" style="font-size: 12px;">
			      <tr>
                  <td>姓名:</td>
                  <td>
				  <input class="easyui-validatebox" name="cusName" required="true" validType="length[1,64]"/>
 			      </td>
                   <td>性别:</td>
                  <td>
                  <input class="easyui-combobox" name="gender" readOnly required="true" id="gender">
                  </td>
 			       <td>出生日期:</td>
                  <td>
				  <input id="birthday" class="easyui-datebox" required="true" name="birthday"/>
 			      </td>
 			      </tr>
    		   </table>
 		  </div>
 		  
 			   <table  id="certificateList" class="easyui-datagrid" title="证件信息" 
			            singleSelect="true" >
                 <thead>
                    <tr>
                     <th field="certTypeId" width="100" hidden=true>
                                                                    证件类型ID
                      </th>
                      <th field="certTypeName" width="100">
                                                                    证件类型
                      </th>
                      <th field="certNo" width="160"  editor="{type:'validatebox',options:{validType:'length[1,64]'}}">证件号码</th>
                      <th field="certExpireDate" width="100" editor="{type:'datebox',options:{editable:false}}">失效日期</th>
                      <th field="updateTime" width="100" formatter="formatDGField">更新日期</th>
                     </tr>
                 </thead>
               </table>
 		  
 		   <div style="height: auto; background: #fafafa; padding: 5px;" class="easyui-panel" title="健康及社会属性" iconCls="icon-save" 
		  collapsible="true" minimizable="false" maximizable="false" closable="false">
			
			   <table cellpadding="5" style="font-size: 12px;">
			      <tr>
                  <td>身高(cm):</td>
                  <td>
				  <input class="easyui-numberbox" precision="2" min="0" name="height" />
 			      </td>
                   <td>体重(kg):</td>
                  <td>
				  <input class="easyui-numberbox" precision="2" min="0" name="weight" />
                  </td>
 			      <td>是否吸烟:</td>
                  <td>
                  <input class="easyui-combobox" name="isSmoking" readOnly  id="isSmoking">
                  </td>
 			      </tr>
 			      
 			       <tr>
                  <td>国籍:</td>
                  <td>
				  <input class="easyui-combobox" name="country" readOnly id="country"/>
 			      </td>
                   <td>民族:</td>
                  <td>
				  <input class="easyui-numberbox" name="nationId" readOnly id="nationId"/>
                  </td>
 			      <td>婚姻状况:</td>
                  <td>
                  <input class="easyui-combobox" name="marriageId" readOnly id="marriageId">
                  </td>
 			      </tr>
 			      
 			       <tr>
                  <td>学历:</td>
                  <td>
				  <input class="easyui-combobox" name="degreeId" readOnly id="degreeId"/>
 			      </td>
                   <td>工作单位:</td>
                  <td>
				  <input class="easyui-validatebox" name="workUnit" validType="length[1,64]" />
                  </td>
 			      <td>年收入（万）:</td>
                  <td>
                  <input class="easyui-numberbox" precision="0" min="0" name="yearIncome" >
                  </td>
 			      </tr>
    		   </table>
 		  </div>
 		  
 		  
			
			    <table class="easyui-datagrid" title="联系信息" 
			          id="contactList"  singleSelect="true"  >
                 <thead>
                    <tr>
                      <th field="conTypeId" width="100" editor="{type:'combobox',options:{editable:false,valueField:'codeId',textField:'codeName',data:contTypeComboData,required:true}}">
                                                                      联系方式类型                                      
                      </th>
                       <th field="contact" width="220" editor="{type:'validatebox',options:{required:true,validType:'length[1,64]'}}">联系方式</th>
                       <th field="updateTime" width="100" formatter="formatDGField">更新日期</th>
                     </tr>
                 </thead>
               </table>
 		  
			    <table class="easyui-datagrid" title="地址信息 "
			          id="addressList"  singleSelect="true"  >
                 <thead>
                    <tr>
                      <th field="addTypeId" width="100" editor="{type:'combobox',options:{editable:false,valueField:'codeId',textField:'codeName',data:addTypeComboData,required:true}}">
                                                                     地址类型	                                 
                      </th>
                       <th field="address" width="300" editor="{type:'validatebox',options:{required:true,validType:'length[1,128]'}}">地址</th>
                       <th field="zip" width="120" editor="{type:'validatebox',options:{validType:'length[6,6]', invalidMessage:'邮编长度必须为6位'}}">邮编</th>
                        <th field="updateTime" width="100" formatter="formatDGField">更新日期</th>
                     </tr>
                 </thead>
               </table>
 		  <center><a class="easyui-linkbutton" onclick="addCustomer()">提交</a><center>
 		  <input type="hidden" name="certRows" id="certRows"></input>
 		  <input type="hidden" name="contRows" id="contRows"></input>
  		  <input type="hidden" name="addrRows" id="addrRows"></input>
 	 </form>
 
    </body>
</html>